package edgeEntity

import (
	"encoding/json"
	"gitee.com/fierce_wolf/go-fox-edge-common/commEntity"
	"strconv"
)

type DeviceRecordEntity struct {
	BaseEntity *commEntity.BaseEntity // 基本信息

	Manufacturer string // 设备厂商名称
	DeviceType   string // 设备类型名
	DeviceName   string // 设备名称

	RecordName string                 // 事件名称
	RecordTime int64                  // 发生时间
	RecordData map[string]interface{} // 配置参数
}

type deviceRecordPo struct {
	Id           int64                  `json:"id" db:"id"`                     // 对象id
	CreateTime   int64                  `json:"createTime" db:"create_time"`    // 创建时间
	UpdateTime   int64                  `json:"updateTime" db:"update_time"`    // 更新时间
	Manufacturer string                 `json:"manufacturer" db:"manufacturer"` // 设备厂商名称
	DeviceType   string                 `json:"deviceType" db:"device_type"`    // 设备类型名
	DeviceName   string                 `json:"deviceName" db:"device_name"`    // 设备名称
	RecordName   string                 `json:"recordName" db:"record_name"`    // 事件名称
	RecordTime   int64                  `json:"recordTime" db:"record_time"`    // 事件时间
	RecordData   map[string]interface{} `json:"recordData" db:"record_data"`    // 扩展参数
}

func (e *DeviceRecordEntity) Type() string {
	return "DeviceRecordEntity"
}

func (e *DeviceRecordEntity) TableName() string {
	return "tb_device_record"
}

func (e *DeviceRecordEntity) GetBaseEntity() *commEntity.BaseEntity {
	return e.BaseEntity
}

func (e *DeviceRecordEntity) Instance() *DeviceRecordEntity {
	return &DeviceRecordEntity{
		BaseEntity: new(commEntity.BaseEntity),
		RecordData: make(map[string]interface{}),
	}
}

func (e *DeviceRecordEntity) NewEntity() commEntity.IEntity {
	return e.Instance()
}

func (e *DeviceRecordEntity) NewPo() interface{} {
	return &deviceRecordPo{
		RecordData: make(map[string]interface{}),
	}
}

func (e *DeviceRecordEntity) MakeServiceKey() string {
	p := e.MakeServiceKeyList()
	return commEntity.MakeServiceKey(p)
}

func (e *DeviceRecordEntity) MakeServiceKeyList() []string {
	var p []string
	p = append(p, strconv.FormatInt(e.BaseEntity.Id, 10))
	return p
}

func (e *DeviceRecordEntity) BuildByJson(jsn *string) error {
	var po deviceRecordPo
	err := json.Unmarshal([]byte(*jsn), &po)
	if err != nil {
		return err
	}

	e.BuildByPo(&po)
	return nil
}

func (e *DeviceRecordEntity) BuildToJson() (string, error) {
	po := e.BuildToPo()
	code, err := json.Marshal(po)
	if err != nil {
		return "", err
	}

	return string(code), nil
}

func (e *DeviceRecordEntity) BuildByPo(pop interface{}) {
	ptr := e
	po := pop.(*deviceRecordPo)

	ptr.BaseEntity.Id = po.Id
	ptr.BaseEntity.CreateTime = po.CreateTime
	ptr.BaseEntity.UpdateTime = po.UpdateTime
	ptr.Manufacturer = po.Manufacturer
	ptr.DeviceType = po.DeviceType
	ptr.DeviceName = po.DeviceName
	ptr.RecordName = po.RecordName
	ptr.RecordTime = po.RecordTime
	ptr.RecordData = po.RecordData
}

func (e *DeviceRecordEntity) BuildToPo() interface{} {
	ptr := e
	po := deviceRecordPo{}

	po.Id = ptr.BaseEntity.Id
	po.CreateTime = ptr.BaseEntity.CreateTime
	po.UpdateTime = ptr.BaseEntity.UpdateTime
	po.Manufacturer = ptr.Manufacturer
	po.DeviceType = ptr.DeviceType
	po.DeviceName = ptr.DeviceName
	po.RecordName = ptr.RecordName
	po.RecordTime = ptr.RecordTime
	po.RecordData = ptr.RecordData

	return &po
}
